package com.dxq.test.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.common.Constant;
import com.dxq.test.R;
import com.dxq.test.adapter.PastureAdapter;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.utils.DialogClickListener;
import com.dxq.test.utils.MyUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/12/1
 * Describe: 牧场列表页面
 */
public class PastureActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle, tvSet;
    private RecyclerView recyclerView;

    private PastureAdapter mAdapter;
    private List<Pasture> recordList = new ArrayList<>();

    private boolean isFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_pasture);
        isFirst = getIntent().getBooleanExtra("isFirst", false);
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvSet = findViewById(R.id.tvSet);
        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("牧场列表");
        tvSet.setText("新建牧场");
        tvSet.setVisibility(View.VISIBLE);

        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new PastureAdapter(R.layout.item_pasture, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件" + position);
                // 弹框选择牧场
                MyUtil.normalDialog(PastureActivity.this, "切换牧场", "是否切换到‘"
                        + recordList.get(position).getName() + "’？", new DialogClickListener() {
                    @Override
                    public void onNegativeClick() {

                    }

                    @Override
                    public void onPositiveClick() {
                        SPUtils.getInstance().put(Constant.PASTURE_ID, recordList.get(position).getPasture_id(), true);
                        App.select_pasture_id = recordList.get(position).getPasture_id();
                        SPUtils.getInstance().put(Constant.PASTURE_NAME, recordList.get(position).getName(), true);
                        App.select_pasture_name = recordList.get(position).getName();
                        SPUtils.getInstance().put(Constant.LABEL_TYPE, Integer.parseInt(recordList.get(position).getType()), true);
                        App.select_label_type = Integer.parseInt(recordList.get(position).getType());
                        if (isFirst) {
                            startActivity(new Intent(PastureActivity.this, MainActivity.class));
                            //清除保存的登录人信息
                            SPUtils.getInstance().remove(Constant.loginUsername, true);
                        }
                        back();
                    }
                });
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");

            }
        });
    }

    private void getData() {
        recordList.clear();
        List<Pasture> pastures = tablesDao.queryPastureAll();
        if (!ObjectUtils.isEmpty(pastures)) {
            for (Pasture pasture : pastures) {
//                List<Label> labels = tablesDao.queryLabelForVaccine(vaccine);
//                if (null != labels && labels.size() > 0)
//                vaccine.setLabel(labels.get(0));
                if (pasture.getPasture_id() == App.select_pasture_id)
                    pasture.setCheck(true);
                recordList.add(pasture);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                back();
                break;

            case R.id.tvSet:
                //addPastureDialog();
                Intent intent = new Intent(PastureActivity.this, PastureAddActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        back();
    }

    private void back() {
        this.finish();
    }

    private void addPastureDialog() {
        View view = getLayoutInflater().inflate(R.layout.add_group_dialog, null);
        TextView tvTitle = view.findViewById(R.id.title);
        tvTitle.setText("牧场名称");
        AutoCompleteTextView tvName = view.findViewById(R.id.name);
        tvName.setHint("填写牧场名称");
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("添加牧场")//设置对话框的标题
                .setView(view)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = tvName.getText().toString();
                        if (name.equals("")) {
                            ToastUtils.showShort("填写牧场名称");
                            return;
                        }
                        //盘点该名称是否已注册
                        List<Pasture> pastures = tablesDao.queryPastureByName(name);
                        if (!ObjectUtils.isEmpty(pastures)) {
                            ToastUtils.showShort("'" + name + "'牧场已经注册");
                            return;
                        }
                        String nowTime = TimeUtils.getNowString();
//                        Pasture pasture = new Pasture(name, nowTime, nowTime, null);
//                        int res = tablesDao.insertPasture(pasture);
//                        if (res != 1) {
//                            ToastUtils.showShort("'" + name + "'牧场注册失败");
//                            return;
//                        }
//                        getData();
                    }
                }).create();
        dialog.show();
    }
}
